package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.CategoryMapper;
import com.goods.business.service.CategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.system.Department;
import com.goods.common.model.system.User;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 产品类别实现类
 * @Author: Mr.Cao
 * @DateTime: 2021/11/8 16:59
 * BMW F750GS
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Map map) {
        //分页查询
        String pageNum = (String) map.get("pageNum");
        String pageSize = (String) map.get("pageSize");
        if (StringUtils.isEmpty(pageNum)&&StringUtils.isEmpty(pageSize)){
            pageNum="1";
            pageSize="10";
        }
        //从数据库中个查询
        List<ProductCategory> productCategoryList = categoryMapper.selectAll(); //查询全部

        if (!CollectionUtils.isEmpty(productCategoryList)){
            //转换成vo
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
            for (ProductCategory productCategory : productCategoryList) {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                //复制
                BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
                //添加到vo集合
                productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
            }
            //构建多级
            List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
            //排序
            //ProductCategoryTreeNodeVO.order();
            //进行分页
            List<ProductCategoryTreeNodeVO> page1 = ListPageUtils.page(build,Integer.valueOf(pageSize), Integer.valueOf(pageNum));

            //返回前段需要的类型
            return new PageVO<>(build.size(), page1);
        }
        return null;
    }

    /**
     * 添加商品时显示父级分类的名称
     * */
    @Override
    public List getParentCategoryTree() {
        //从数据库中个查询
        List<ProductCategory> productCategoryList = categoryMapper.selectAll(); //查询全部
        if (!CollectionUtils.isEmpty(productCategoryList)) {
            //转换成vo
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
            for (ProductCategory productCategory : productCategoryList) {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                //复制
                BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
                //添加到vo集合
                productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
            }
            //构建多级
            List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);

            return build;
        }
        return null;
    }
    /**
     *添加分类
     */
    @Override
    public void add(ProductCategoryVO productCategoryVO) throws BusinessException {
        String categoryName = productCategoryVO.getName();
         Long categoryNamePid = productCategoryVO.getPid();
        if (StringUtils.isEmpty(categoryName)){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"分类名称不能为空");
        }
        //转换
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        //添加
        categoryMapper.insert(productCategory);
    }

    /**
     * 删除分类
     * */
    @Override
    public void delete(long categoryId) throws BusinessException {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(categoryId);
        //查询
        ProductCategory productCategoryById = categoryMapper.selectOne(productCategory);
        //查新子集分类
        //Long pid = productCategoryById.getPid();
        Example o = new Example(ProductCategory.class);
        o.createCriteria().andEqualTo("pid",productCategoryById.getId());
        System.out.println("查出来的总记录数为: "+categoryMapper.selectCountByExample(o));
        if (categoryMapper.selectCountByExample(o)>0){
            //有子节点
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"请先删除下级分类");
        }else {
            //没有子节点,可以删除
            categoryMapper.deleteByPrimaryKey(categoryId);
        }
    }
    /**
     * 编辑时显示分类信息
     * */
    @Override
    public ProductCategory edit(long categoryId) {
        ProductCategory productCategory = categoryMapper.selectByPrimaryKey(categoryId);
        return productCategory;
    }

    @Override
    public void update(long categoryId,ProductCategoryVO productCategoryVO) {
        //先根据Id查询
        ProductCategory productCategoryById = categoryMapper.selectByPrimaryKey(categoryId);
        if (productCategoryById!=null){
            BeanUtils.copyProperties(productCategoryVO,productCategoryById);
            productCategoryById.setModifiedTime(new Date());
            categoryMapper.updateByPrimaryKey(productCategoryById);
        }
    }
}
